package handler

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"order-pro/cache"
	"order-pro/model"
	"strconv"
	"time"
)

func RepoOrderDetail(c *gin.Context) {
	id, _ := strconv.Atoi(c.Query("id"))
	redis_key := "repo_order_detail_" + strconv.Itoa(id)
	var list model.RepoOrderList
	/*
		SELECT
		  repos.repo_code RepoCode,
		  repos.repo_name RepoName,
		  repo_orders.order_code OrderCode,
		  repo_orders.order_price OrderPrice
		FROM
		  repo_orders
		  LEFT JOIN repos ON repos.id = repo_orders.rid
		WHERE repo_orders.id=1
	*/
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		model.DB.Table("repo_orders").
			Select(
				"repos.repo_code RepoCode",
				"repos.repo_name RepoName",
				"repo_orders.order_code OrderCode",
				"repo_orders.order_price OrderPrice",
			).
			Joins("LEFT JOIN repos ON repos.id = repo_orders.rid").
			Where("repo_orders.id=?", id).Find(&list)
		jsonData, _ := json.Marshal(list)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &list)
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "ok",
		"data": list,
	})
	return
}

func RepoOrderList(c *gin.Context) {
	page, _ := strconv.Atoi(c.Query("page"))
	size, _ := strconv.Atoi(c.Query("size"))
	offset := (page - 1) * size
	var total int64
	model.DB.Model(&model.RepoOrder{}).Count(&total)
	redis_key := "repo_order_list_" + strconv.Itoa(page)
	var list []model.RepoOrderList
	/*
		SELECT
		  repos.repo_code RepoCode,
		  repos.repo_name RepoName,
		  repo_orders.order_code OrderCode,
		  repo_orders.order_price OrderPrice
		FROM
		  repo_orders
		  LEFT JOIN repos ON repos.id = repo_orders.rid
		WHERE repo_orders.id=1
	*/
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		model.DB.Table("repo_orders").
			Select(
				"repos.repo_code RepoCode",
				"repos.repo_name RepoName",
				"repo_orders.order_code OrderCode",
				"repo_orders.order_price OrderPrice",
			).
			Joins("LEFT JOIN repos ON repos.id = repo_orders.rid").
			Offset(offset).Limit(size).Order("repo_orders.id desc").Find(&list)
		jsonData, _ := json.Marshal(list)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &list)
	}
	c.JSON(200, gin.H{
		"code":  200,
		"msg":   "ok",
		"data":  list,
		"total": total,
	})
	return
}

func RepoOrderAdd(c *gin.Context) {
	rid, _ := strconv.Atoi(c.PostForm("rid"))
	order_code := c.PostForm("order_code")
	order_price := c.PostForm("order_price")
	data := model.RepoOrder{
		Rid:        rid,
		OrderCode:  order_code,
		OrderPrice: order_price,
	}
	if err := model.DB.Create(&data).Error; err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "入库订单添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "入库订单添加成功",
	})
	return
}

func RepoAdd(c *gin.Context) {
	repo_code := c.PostForm("repo_code")
	repo_name := c.PostForm("repo_name")
	data := model.Repo{
		RepoCode: repo_code,
		RepoName: repo_name,
	}
	if err := model.DB.Create(&data).Error; err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "仓库添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "仓库添加成功",
	})
	return
}
